<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>选宿舍表单</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
    <link rel="stylesheet" href="/css/ui.css"/>
    <script src="/js/ui.js"></script>
</head>
<body>
<div class="page__bd">
    <div class="weui-form">
        <div class="weui-form__text-area">
            <h2 class="weui-form__title">男生剩余宿舍</h2>
        </div>
        <div class="weui-form__control-area">
            <div class="weui-cells__group weui-cells__group_form">
                <div class="weui-cells weui-cells_form">
                    <label for="building_5_man" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">五号楼</span></div>
                        <div class="weui-cell__bd">
                            <input id="building_5_man" type="text" class="weui-input" placeholder="0" readonly/>
                        </div>
                    </label>
                    <label for="building_8_man" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">八号楼</span></div>
                        <div class="weui-cell__bd">
                            <input id="building_8_man" type="text" class="weui-input" placeholder="0" readonly/>
                        </div>
                    </label>
                    <label for="building_13_man" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">十三号楼</span></div>
                        <div class="weui-cell__bd">
                            <input id="building_13_man" type="text" class="weui-input" placeholder="0" readonly/>
                        </div>
                    </label>
                </div>
            </div>
        </div>

        <div class="weui-form__text-area">
            <h2 class="weui-form__title">女生剩余宿舍</h2>
        </div>
        <div class="weui-form__control-area">
            <div class="weui-cells__group weui-cells__group_form">
                <div class="weui-cells weui-cells_form">
                    <label for="building_5_woman" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">五号楼</span></div>
                        <div class="weui-cell__bd">
                            <input id="building_5_woman" type="text" class="weui-input" placeholder="0" readonly/>
                        </div>
                    </label>
                    <label for="building_8_woman" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">八号楼</span></div>
                        <div class="weui-cell__bd">
                            <input id="building_8_woman" type="text" class="weui-input" placeholder="0" readonly/>
                        </div>
                    </label>
                    <label for="building_13_woman" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">十三号楼</span></div>
                        <div class="weui-cell__bd">
                            <input id="building_13_woman" type="text" class="weui-input" placeholder="0" readonly/>
                        </div>
                    </label>
                </div>
            </div>
        </div>

        <div class="weui-form__tips-area">
            <p class="weui-form__tips" id="search_result">
            </p>
        </div>
        <div class="weui-form__opr-area">
            <a role="button" id="search_remain" class="weui-btn weui-btn_primary">查询</a>
        </div>
    </div>

    <div class="weui-form">
        <div class="weui-form__text-area">
            <h2 class="weui-form__title">选宿舍表单</h2>
        </div>
        <div class="weui-form__control-area">
            <div class="weui-cells__group weui-cells__group_form">
                <div class="weui-cells weui-cells_form">
                    <label for="student_no" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">你的学号</span></div>
                        <div class="weui-cell__bd">
                            <input id="student_no" type="text" class="weui-input" placeholder="你的学号" readonly/>
                        </div>
                    </label>
                    <label for="student_verify_code" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">你的认证码</span></div>
                        <div class="weui-cell__bd">
                            <input id="student_verify_code" type="text" class="weui-input" placeholder="你的认证码"
                                   readonly/>
                        </div>
                    </label>
                    <br>
                    <label for="student1_id" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">同住人1的学号</span></div>
                        <div class="weui-cell__bd">
                            <input id="student1_id" type="text" class="weui-input"/>
                        </div>
                    </label>
                    <label for="student1_code" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">同住人1的认证码</span></div>
                        <div class="weui-cell__bd">
                            <input id="student1_code" type="text" class="weui-input"/>
                        </div>
                    </label>
                    <br>
                    <label for="student2_id" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">同住人2的学号</span></div>
                        <div class="weui-cell__bd">
                            <input id="student2_id" type="text" class="weui-input"/>
                        </div>
                    </label>
                    <label for="student2_code" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">同住人2的认证码</span></div>
                        <div class="weui-cell__bd">
                            <input id="student2_code" type="text" class="weui-input"/>
                        </div>
                    </label>
                    <br>
                    <label for="student3_id" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">同住人3的学号</span></div>
                        <div class="weui-cell__bd">
                            <input id="student3_id" type="text" class="weui-input"/>
                        </div>
                    </label>
                    <label for="student3_code" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">同住人3的认证码</span></div>
                        <div class="weui-cell__bd">
                            <input id="student3_code" type="text" class="weui-input"/>
                        </div>
                    </label>

                    <label for="buildingName" class="weui-cell weui-cell_active">
                        <div class="weui-cell__hd"><span class="weui-label">目标楼号</span></div>
                        <div class="weui-cell__bd">
                            <select id="buildingName">
                                <option value="五号楼">五号楼</option>
                                <option value="八号楼">八号楼</option>
                                <option value="十三号楼">十三号楼</option>
                            </select>
                        </div>
                    </label>
                </div>
            </div>
        </div>
        <div class="weui-form__tips-area">
            <p class="weui-form__tips" id="result">
            </p>
        </div>
        <div class="weui-form__opr-area">
            <a role="button" id="btn1" class="weui-btn weui-btn_primary">确定</a>
        </div>
    </div>
    <div class="button-sp-area cell">
        <a id="logout" role="button" class="weui-btn_cell weui-btn_cell-default">登出</a>
    </div>
</div>
</body>

<script type="text/javascript">
    $(document).ready(function () {
        if (localStorage.getItem("token") == null) {
            alert("请先登录!");
            logout();
        }
        var submitter_id = localStorage.getItem("studentNo");
        var submitter_code = localStorage.getItem("verifyCode");
        $("#student_no").attr("value", submitter_id);
        $("#student_verify_code").attr("value", submitter_code);
        console.log(submitter_id, submitter_code);
        loadDormRemain();
        $("#btn1").click(function () {
            let building_name = $("#buildingName").val();
            let followers_id = [], followers_code = [];
            for (let i = 1; i <= 3; ++i) {
                let follower_id = $("#student" + i + "_id").val();
                let follower_code = $("#student" + i + "_code").val();
                followers_id[i - 1] = follower_id;
                followers_code[i - 1] = follower_code;
                console.log(follower_id + " : " + follower_code);
            }
            let token = localStorage.getItem("token");
            console.log(token);
            let paramData = {
                studentNos: followers_id,
                codes: followers_code,
                submitterId: submitter_id,
                submitterCode: submitter_code,
                buildingName: building_name
            };
            $.ajax({
                type: 'post',
                url: "http://59.110.168.33:8080/order",
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                data: JSON.stringify(paramData),
                beforeSend: function (request) {
                    request.setRequestHeader("token", token);
                },
                success: function (data) {
                    console.log(data);
                    // $("#result").text(data.msg);
                    if (data.code === 200) {
                        getResult(submitter_id);
                        // loadDormRemain();
                    } else {
                        $("#result").text(data.msg);
                    }
                },
                error: function (data) {
                    alert("请先登录");
                    logout();
                }
            });
        });
        $("#search_remain").click(function () {
            loadDormRemain();
        });

        $("#logout").click(function () {
            logout();
        });

        function getResult(studentNo) {
            //一个转圈图标
            $("#result").text("订单提交成功，排队中");
            let param = {
                studentNo: studentNo
            }
            $.ajax({
                type: "post",
                url: "http://59.110.168.33:8080/result",
                contentType:'application/x-www-form-urlencoded',
                dataType: 'json',
                data: JSON.stringify(param),
                success: function (data) {
                    console.log(data)
                    if (data.code === 200) {
                        let result = data.obj.status;
                        if (result === -1) {
                            alert("对不起，选宿舍失败");
                        } else if(result===0) {
                            setTimeout(function() {
                                getResult(studentNo);
                            }, 50);
                        } else {
                            $("#result").text(data.obj.msg);
                        }
                    }
                }
            })
        }

        function logout() {
            localStorage.clear();
            sessionStorage.clear();
            location.href = "http://59.110.168.33:8080/login";
        }

        function loadDormRemain() {
            $.ajax({
                type: 'get',
                url: "http://59.110.168.33:8080/list",
                beforeSend: function (request) {
                    request.setRequestHeader("token", localStorage.getItem("token"));
                },
                success: function (data) {
                    console.log(data);
                    data = JSON.parse(data);
                    if (data.code === 200) {
                        $("#building_5_man").attr("value", data.obj.五号楼男);
                        $("#building_8_man").attr("value", data.obj.八号楼男);
                        $("#building_13_man").attr("value", data.obj.十三号楼男);
                        $("#building_5_woman").attr("value", data.obj.五号楼女);
                        $("#building_8_woman").attr("value", data.obj.八号楼女);
                        $("#building_13_woman").attr("value", data.obj.十三号楼女);
                    }
                },
                error: function (data) {
                    console.log(data);
                    $("#search_result").text("请先登录!");
                    logout();
                }
            });
        }
    });
</script>
</html>